home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue52 / Alfresco / TstBits.dpr < prev    next >
Encoding:
Text File  |  1999-10-31  |  3.5 KB  |  136 lines

  1. program TstBits;
  2.  
  3. {$IFDEF Win32}
  4. {$APPTYPE CONSOLE}
  5. {$ENDIF}
  6.  
  7. uses
  8.   {$IFDEF Windows}
  9.   WinCrt,
  10.   {$ENDIF}
  11.   SysUtils,
  12.   AABitSet in 'AABitSet.pas';
  13.  
  14. var
  15.   MyBitSet    : TaaBitSet;
  16.   OtherBitSet : TaaBitSet;
  17.   i           : integer;
  18. begin
  19.   writeln('Testing bitset methods');
  20.   MyBitSet := nil;
  21.   OtherBitSet := nil;
  22.   try
  23.     MyBitSet := TaaBitSet.Create(21);
  24.  
  25.     {set every other bit}
  26.     writeln('..setting bits');
  27.     for i := 0 to 20 do
  28.       if Odd(i) then
  29.         MyBitSet[i] := true;
  30.     for i := 0 to 20 do
  31.       if Odd(i) then begin
  32.         if not MyBitSet[i] then
  33.           writeln('Error at ', i);
  34.       end
  35.       else begin
  36.         if MyBitSet[i] then
  37.           writeln('Error at ', i);
  38.       end;
  39.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  40.  
  41.     {not the bitset}
  42.     writeln('..not bitset');
  43.     MyBitSet.NotBitSet;
  44.     for i := 0 to 20 do
  45.       if not Odd(i) then begin
  46.         if not MyBitSet[i] then
  47.           writeln('Error at ', i);
  48.       end
  49.       else begin
  50.         if MyBitSet[i] then
  51.           writeln('Error at ', i);
  52.       end;
  53.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  54.  
  55.     {clear every other bit}
  56.     writeln('..clearing bits');
  57.     for i := 0 to 20 do
  58.       if not Odd(i)then
  59.         MyBitSet[i] := false;
  60.     for i := 0 to 20 do
  61.       if MyBitSet[i] then
  62.         writeln('Error at ', i);
  63.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  64.  
  65.     {set and clear all bits}
  66.     writeln('..set and then clear all bits');
  67.     MyBitSet.SetAllBits;
  68.     for i := 0 to 20 do
  69.       if not MyBitSet[i] then
  70.         writeln('Error at ', i);
  71.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  72.     MyBitSet.ClearAllBits;
  73.     for i := 0 to 20 do
  74.       if MyBitSet[i] then
  75.         writeln('Error at ', i);
  76.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  77.  
  78.     {and, or, xor with another bitset}
  79.     writeln('..and with other bitset');
  80.     OtherBitSet := TaaBitSet.Create(21);
  81.     for i := 0 to 20 do
  82.       if Odd(i) then
  83.         OtherBitSet[i] := true;
  84.     for i := 0 to 20 do
  85.       if (i mod 5) = 0 then
  86.         MyBitSet[i] := true;
  87.     MyBitSet.AndBitSet(OtherBitSet);
  88.     writeln('set bits should be 5 and 15');
  89.     for i := 0 to 20 do
  90.       if MyBitSet[i] then
  91.         write(i:3);
  92.     writeln;
  93.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  94.  
  95.     writeln('..or with other bitset');
  96.     MyBitSet.ClearAllBits;
  97.     for i := 0 to 20 do
  98.       if (i mod 5) = 0 then
  99.         MyBitSet[i] := true;
  100.     MyBitSet.OrBitSet(OtherBitSet);
  101.     writeln('set bits should be 0, 1, 3, 5, 7, 9, 10, 11, 13, 15, 17, 19, 20');
  102.     for i := 0 to 20 do
  103.       if MyBitSet[i] then
  104.         write(i:3);
  105.     writeln;
  106.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  107.  
  108.     writeln('..xor with other bitset');
  109.     MyBitSet.ClearAllBits;
  110.     for i := 0 to 20 do
  111.       if (i mod 5) = 0 then
  112.         MyBitSet[i] := true;
  113.     MyBitSet.XorBitSet(OtherBitSet);
  114.     writeln('set bits should be 0, 1, 3, 7, 9, 10, 11, 13, 17, 19, 20');
  115.     for i := 0 to 20 do
  116.       if MyBitSet[i] then
  117.         write(i:3);
  118.     writeln;
  119.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  120.  
  121.     {get the next eleven 'free' bits}
  122.     writeln('..get the next eleven bits');
  123.     writeln('  should be 2, 4, 5, 6, 8, 12, 14, 15, 16, 18, -1');
  124.     for i := 1 to 11 do
  125.       write(MyBitSet.GetNextBit:3);
  126.     writeln;
  127.     writeln('SetBitCount: ', MyBitSet.SetBitCount);
  128.  
  129.  finally
  130.     MyBitSet.Free;
  131.     OtherBitSet.Free;
  132.   end;
  133.   writeln('Done: press Enter');
  134.   readln;
  135. end.
  136.